//
//  MyCollectionViewCell.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/22.
//  Copyright © 2017年 Lwx. All rights reserved.
//
#define border 10
#import "MyCollectionViewCell.h"


@interface MyCollectionViewCell()
@property (nonatomic, strong)UIImageView * bgImageView;
@property (nonatomic, strong)UIImageView * locationImage;
@property (nonatomic, strong)UILabel * locationLab;
@property (nonatomic, strong)UILabel * contentLab;
@property (nonatomic, strong)UILabel * moneyLab;
@property (nonatomic, strong)UIButton * heartBtn;
@property (nonatomic, strong)NSString * tid;//tid 是游记、旅游信息、活动信息id
@end

@implementation MyCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bgImageView];
        [self.bgImageView addSubview:self.locationImage];
        [self.bgImageView addSubview:self.locationLab];
        [self.contentView addSubview:self.contentLab];
        [self.contentView addSubview:self.moneyLab];
        [self.contentView addSubview:self.heartBtn];
    }return self;
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
//        _bgImageView.backgroundColor = [UIColor redColor];
        _bgImageView.layer.cornerRadius = 6;
        _bgImageView.clipsToBounds = YES;
    }return _bgImageView;
}

- (UIImageView *)locationImage{
    if (!_locationImage) {
        _locationImage = [[UIImageView alloc] init];
        _locationImage.image = [UIImage imageNamed:@"yj_location"];
        
    }return _locationImage;
}

- (UILabel *)locationLab{
    if (!_locationLab) {
        _locationLab = [[UILabel alloc] init];
        _locationLab.textAlignment = NSTextAlignmentLeft;
        _locationLab.font = DEF_FontSize_10;
        _locationLab.text = @"XX区XX号";
//        _locationLab.backgroundColor = [UIColor yellowColor];
    }return _locationLab;
}

- (UILabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] init];
        _contentLab.text = @"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        _contentLab.numberOfLines = 0;
        _contentLab.lineBreakMode = NSLineBreakByWordWrapping;
        _contentLab.font = DEF_FontSize_14;
        _contentLab.textAlignment = NSTextAlignmentLeft;
    }return _contentLab;
}

- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.text = @"￥2333";
        _moneyLab.font = DEF_FontSize_14;
        _moneyLab.textAlignment = NSTextAlignmentLeft;
        _moneyLab.textColor = [UIColor redColor];
    }return _moneyLab;
}

- (UIButton *)heartBtn{
    if (!_heartBtn) {
        _heartBtn = [[UIButton alloc] init];
        [_heartBtn setImage:[UIImage imageNamed:@"collect_heart"] forState:UIControlStateNormal];
//        [_heartBtn setTitle:@"哈哈" forState:UIControlStateNormal];
        _heartBtn.titleLabel.font = DEF_FontSize_14;
        [_heartBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //button图片的偏移量，距上左下右分别(10, 10, 10, 60)像素点
        _heartBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 80);
        [_heartBtn setTitle:@"111" forState:UIControlStateNormal];
        _heartBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
//        //button标题的偏移量，这个偏移量是相对于图片的
        _heartBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -60, 0, 0);
        

    }return _heartBtn;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    int locationImageH = 10;
    int contentLabH = 20;
    int moneyW = 80;
    _bgImageView.frame = CGRectMake(border, border, self.contentView.width - 2 * border, 100);
    _locationImage.frame = CGRectMake(border, _bgImageView.height - locationImageH - border, locationImageH, locationImageH);
    int locationLabW = _bgImageView.width - _locationImage.right - border;
    _locationLab.frame = CGRectMake(_locationImage.right, _locationImage.top, locationLabW, _locationImage.height);
    _contentLab.frame = CGRectMake(_bgImageView.left, _bgImageView.bottom + border, _bgImageView.width, contentLabH * 2);
    _moneyLab.frame = CGRectMake(_bgImageView.left, _contentLab.bottom + border, moneyW, contentLabH);
    _heartBtn.frame = CGRectMake(_bgImageView.left, _contentLab.bottom + border, 100, 18);
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.locationLab.text = model[@"location"];
    self.contentLab.text = model[@"content"];
    self.bgImageView.image = [UIImage imageNamed:model[@"image"]];
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@",model[@"money"]];
    self.tid = model[@"tid"];
}

- (void)setModelData:(MyCollectionModelData *)modelData{
    _modelData = modelData;
    self.tid = modelData.tid;
    [self.heartBtn setTitle:nil forState:UIControlStateNormal];
    self.moneyLab.text = nil;
    self.locationLab.text = modelData.address;
    self.contentLab.text = modelData.title;
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:modelData.pic]];
    if (modelData.likes == nil) {
        self.heartBtn.hidden = YES;
        self.moneyLab.hidden = NO;
        self.moneyLab.text = modelData.price;
    }else{
        self.heartBtn.hidden = NO;
        self.moneyLab.hidden = YES;
        [self.heartBtn setTitle:modelData.likes forState:UIControlStateNormal];
    }
    
}

@end
